using System;
using System.Windows;
using DeSleeper.Client.Properties;
using DeSleeper.Client.WakeOnLanService;
using DeSleeper.Library;

namespace DeSleeper.Client.WakeUp
{
    public class MacAddressWakeUpMethod : WakeUpMethod
    {
        public MacAddressWakeUpMethod()  {}
        public MacAddressWakeUpMethod(TargetComputer target) : base(target) { }

        #region MacAddress Attached Property
        public static readonly DependencyProperty MacAddressProperty =
            DependencyProperty.RegisterAttached("MacAddress", typeof(string), typeof(MacAddressWakeUpMethod));

        public static string GetMacAddress(TargetComputer target)
        {
            return (string)target.GetValue(MacAddressProperty);
        }

        public static void SetMacAddress(TargetComputer target, string value)
        {
            target.SetValue(MacAddressProperty, value);
        }

        protected string MacAddress
        {
            get { return GetMacAddress(Target); }
            set { SetMacAddress(Target, value); }
        }
        #endregion

        /// <summary>
        /// A description of the target used to identify it to the user.
        /// </summary>
        public override string Description { get { return "Wake up using a MAC address."; } }

        /// <summary>
        /// Identifier used to identify target to code.
        /// </summary>
        public override string Id { get { return "MacAddress"; } }

        /// <summary>
        /// Does the assigned target has enough and correct information to perform a wake
        /// up with this method?
        /// </summary>
        public override bool IsValid
        {
            get
            {
                if (!base.IsValid) return false;
                return WakeOnLanHelper.ValidateMacAddress(MacAddress);
            }
        }

        protected override void WakeUpCore(WakeUpServiceClient serviceClient)
        {
            if (!IsValid)
                throw new InvalidOperationException(Resources.ErrorInvalidMACAddress);
            serviceClient.WakeUp(MacAddress);
        }

        ///<summary>
        /// Creates a new instance to support cloning.
        ///</summary>
        protected override Freezable CreateInstanceCore()
        {
            return new MacAddressWakeUpMethod(Target);
        }
    }
}